package org.rambadger.command.resource;


import com.yammer.dropwizard.auth.Auth;
import com.yammer.dropwizard.views.View;
import org.mongojack.DBCursor;
import org.mongojack.DBQuery;
import org.mongojack.DBSort;
import org.mongojack.JacksonDBCollection;
import org.rambadger.command.auth.AuthUser;
import org.rambadger.command.axon.JCollection;
import org.rambadger.command.view.IndexView;
import org.rambadger.command.view.LoginView;
import org.rambadger.command.view.RegisterUserForm;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/admin")
@Produces({MediaType.TEXT_HTML})
public class AdminResource {
    @JCollection(type=AuthUser.class)
    private JacksonDBCollection<AuthUser, String> authUsers = null;

    //DBCursor<AuthUser> found = authUsers.findOne();

    @GET
    public View Index(@Auth(required = false) AuthUser user){
        DBCursor<AuthUser> found = authUsers.find(DBQuery.is("hasSystemAdmin", true));

        if (found.count() == 0)
            return new RegisterUserForm(user);

        else if (user == null)
            return new LoginView();

        else if (user.hasAdmin())
            return new IndexView(user);

        else {
            return new IndexView(user);
        }
    }
}
